<script setup>
import coachApi from '@/api/coachApi';
import { reactive, ref } from 'vue';
import { ElMessage, ElMessageBox } from 'element-plus'
import service from '@/api';

const coachs = ref([]);

// 分页信息
const pageInfo = reactive({
    page: 1,
    limit: 10,
    total: 0,
})

// 获取教练列表
loadCoachs();

// 页码和页面大小更新
function loadCoachs() {
    coachApi.search(pageInfo).then(result => {
        coachs.value = result.data.list;
        pageInfo.total = result.data.total;
    });
}

// 添加的相关操作
const addVisible = ref(false);
const addCoach = ref({});
function showAdd() {
    addVisible.value = true;    // 显示添加窗口
}

function handleAdd(){
    coachApi.add(addCoach.value).then(result=>{
        if (result.code == 0) {
            ElMessage({ message: '教练创建成功！', type: 'success' })
            addVisible.value = false;
            addCoach.value = {};
        } else {
            ElMessage.error(result.msg);
        }
        // 刷新表格数据
        loadCoachs();
    });
}

// 编辑的操作
const editVisible = ref(false);
const editCoach = ref({});

function showEdit(row){
    editVisible.value = true;
    //editCoach.value = Object.assign({}, row);   // 如果你有一个对象，再创建一个一样的对象？？？克隆
    //editCoach.value = {...row}; // 作用同上
    editCoach.value = JSON.parse(JSON.stringify(row));   // 深拷贝
    editCoach.value.password = "";
    console.log(row);
}

function handleEdit(){
    coachApi.edit(editCoach.value).then(result=>{
        if (result.code == 0) {
            ElMessage({ message: '编辑教练成功！', type: 'success' })
            editVisible.value = false;
            editCoach.value = {};
        } else {
            ElMessage.error(result.msg);
        }
        // 刷新表格数据
        loadCoachs();
    });
}

function handleRemove(coach){
    ElMessageBox.confirm(
        '亲，确定要移除当前练么?',
        '警告',
        {
            confirmButtonText: '确定',
            cancelButtonText: '不了',
            type: 'warning',
        }
    ).then(()=>{
        // 确定
        coachApi.remove(coach.id).then(result=>{
            if (result.code == 0) {
                ElMessage({ message: '移除教练成功！', type: 'success' })
            } else {
                ElMessage.error(result.msg);
            }
            // 刷新表格数据
            loadCoachs();
        });
    });
    
}

function handleAddAvatarSuccess(response, uploadFile){
    addCoach.value.photo = response.msg;
}
function handleEditAvatarSuccess(response, uploadFile){
    editCoach.value.photo = response.msg;
}

function getImgUrl(url){
    return service.defaults.baseURL + url;
}
function getUploadURL(){
    return service.defaults.baseURL + '/upload';
}

function onSearch(){
    pageInfo.page = 1;
    loadCoachs();
}

</script>

<template>
    <div class="content">
        <el-form :model="pageInfo" :inline="true">
            <el-form-item label="姓名" :label-width="60">
                <el-input v-model="pageInfo.name" clearable placeholder="请输入教练名字"/>
            </el-form-item>
            <el-form-item label="手机号" :label-width="60">
                <el-input v-model="pageInfo.phone" clearable placeholder="请输入教练手机"/>
            </el-form-item>
            <el-form-item>
                <el-button type="primary" @click="onSearch"><el-icon><Search></Search></el-icon></el-button>
            </el-form-item>
        </el-form>
        <div class="bar">
            <el-button type="success" @click="showAdd" >创建教练</el-button>
        </div>
        <el-table :data="coachs" :stripe="true" :border="true" style="width: 100%">
            <el-table-column prop="id" label="ID" width="80" />
            <el-table-column prop="photo" label="照骗" >
                <template #default="scope">
                    <img :src="getImgUrl(scope.row.photo)" style="max-height: 60px; max-width: 120px;"/>
                </template>
            </el-table-column>
            <el-table-column prop="name" label="姓名" />
            <el-table-column prop="phone" label="手机号" width="180" />
            <el-table-column prop="wechat" label="微信" />
            <el-table-column prop="recomm" label="简介" />
            <el-table-column label="操作">
                <template #default="scope">
                    <el-button size="small" type="success"
                        @click="showEdit(scope.row)">编辑</el-button> 
                    <el-button size="small" type="danger"
                        @click="handleRemove(scope.row)">删除</el-button> 
                </template>
            </el-table-column>
        </el-table>
        <el-pagination class="page" v-model:current-page="pageInfo.page" v-model:page-size="pageInfo.limit"
            :total="pageInfo.total" :page-sizes="[10, 20, 50, 100]" :background="true"
            layout="total, sizes, prev, pager, next, jumper" @change="loadCoachs" />
    </div>

    <!-- 添加的弹窗 -->
    <el-dialog v-model="addVisible" title="创建新教练" width="500">
        <el-form :model="addCoach">
            <el-form-item label="照片" :label-width="60">
                <el-upload
                    class="avatar-uploader"
                    :action="getUploadURL()"
                    :show-file-list="false"
                    :on-success="handleAddAvatarSuccess"
                >
                    <img v-if="addCoach.photo" :src="getImgUrl(addCoach.photo)" class="avatar" />
                    <el-icon v-else class="avatar-uploader-icon"><Plus /></el-icon>
                </el-upload>
            </el-form-item>
            <el-form-item label="名字" :label-width="60">
                <el-input v-model="addCoach.name" placeholder="请输入教练名字"/>
            </el-form-item>
            <el-form-item label="电话" :label-width="60">
                <el-input v-model="addCoach.phone" placeholder="请输入教练电话"/>
            </el-form-item>
            <el-form-item label="微信" :label-width="60">
                <el-input v-model="addCoach.wechat" placeholder="请输入教练微信"/>
            </el-form-item>
            <el-form-item label="简介" :label-width="60">
                <el-input
                    :rows = "5"
                    maxlength="255"
                    placeholder="请输入教练简介"
                    show-word-limit
                    type="textarea"
                    v-model="addCoach.recomm"
                    autocomplete="off" />
            </el-form-item>
        </el-form>
        <template #footer>
            <div class="dialog-footer">
                <el-button @click="addVisible = false">取消</el-button>
                <el-button type="primary" @click="handleAdd">
                    创建
                </el-button>
            </div>
        </template>
    </el-dialog>

    <!-- 编辑的弹窗 -->
    <el-dialog v-model="editVisible" title="编辑教练" width="500">
        <el-form :model="editCoach">
            <el-form-item label="照片" :label-width="60">
                <el-upload
                    class="avatar-uploader"
                    :action="getUploadURL()"
                    :show-file-list="false"
                    :on-success="handleEditAvatarSuccess"
                >
                    <img v-if="editCoach.photo" :src="getImgUrl(editCoach.photo)" class="avatar" />
                    <el-icon v-else class="avatar-uploader-icon"><Plus /></el-icon>
                </el-upload>
            </el-form-item>
            <el-form-item label="名字" :label-width="60">
                <el-input v-model="editCoach.name" placeholder="请输入教练名字"/>
            </el-form-item>
            <el-form-item label="电话" :label-width="60">
                <el-input v-model="editCoach.phone" placeholder="请输入教练电话"/>
            </el-form-item>
            <el-form-item label="微信" :label-width="60">
                <el-input v-model="editCoach.wechat" placeholder="请输入教练微信"/>
            </el-form-item>
            <el-form-item label="简介" :label-width="60">
                <el-input
                    :rows = "5"
                    maxlength="255"
                    placeholder="请输入教练简介"
                    show-word-limit
                    type="textarea"
                    v-model="editCoach.recomm"
                    autocomplete="off" />
            </el-form-item>
        </el-form>
        <template #footer>
            <div class="dialog-footer">
                <el-button @click="editVisible = false">取消</el-button>
                <el-button type="primary" @click="handleEdit">
                    编辑
                </el-button>
            </div>
        </template>
    </el-dialog>
</template>

<style >

</style>